#include "../mq_server/exchange.h"
#include <gtest/gtest.h>
using namespace ns_exchange;

shared_ptr<ExchangeManage> exmp;

class ExchangeTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        exmp = make_shared<ExchangeManage>("test/test.db");
        shared_ptr<Exchange> exp = exmp->select_exchange("e1");
    }

    virtual void TearDown() override
    {
        exmp->clear();
    }
};

TEST(ExchangeTest, insert_test)
{
    unordered_map<string, string> hash;
    hash["key1"] = "val1";
    hash["key2"] = "val2";
    exmp->declare_exchange("e1", FANOUT, true, false, hash);
    exmp->declare_exchange("e2", FANOUT, true, false, unordered_map<string, string>());
    exmp->declare_exchange("e3", FANOUT, true, false, hash);
    ASSERT_EQ(exmp->size(), 3);
}

TEST(ExchangeTest, select_test)
{
    shared_ptr<Exchange> exp = exmp->select_exchange("e1");
    ASSERT_NE(exp.get(),nullptr);
    ASSERT_EQ(exp->_name, string("e1"));
    ASSERT_EQ(exp->_type, FANOUT);
    ASSERT_EQ(exp->_IsDurable, true);
    ASSERT_EQ(exp->_auto_delete, false);
    ASSERT_EQ(exp->_args.size(),2);
    ASSERT_EQ(exp->_args["key1"], string("val1"));
    ASSERT_EQ(exp->get_args(), string("key2=val2&key1=val1"));
}

TEST(ExchangeTest, delete_test)
{
    exmp->exist("e1");
    exmp->exist("e2");
    exmp->delete_exchange("e1");
    exmp->delete_exchange("e2");
    ASSERT_EQ(exmp->size(), 1);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);

    return RUN_ALL_TESTS();
}